package com.xja.dubbo.service.impl;

import com.xja.dubbo.entity.*;
import com.xja.dubbo.mapper.Easybuy_productMapper;
import com.xja.dubbo.mapper.Easybuy_product_categoryMapper;
import com.xja.dubbo.mapper.Easybuy_product_imagesMapper;
import com.xja.dubbo.service.ProductService;
import com.xja.dubbo.util.JsonUtil;
import net.minidev.json.JSONUtil;
import org.apache.dubbo.config.annotation.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.security.Key;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ycz
 * @create 2021-12-15-10:05
 */
@Component
@Service(interfaceClass = ProductService.class)
public class ProductServiceImpl implements ProductService {

    private Logger logger = LoggerFactory.getLogger(ProductServiceImpl.class);

    @Autowired
    private Easybuy_productMapper easybuy_productMapper;

    @Autowired
    private Easybuy_product_imagesMapper easybuy_product_imagesMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void addProduct(Easybuy_product easybuy_product, List<String> productImageString) throws Exception {
        logger.info("添加了商品，同时添加商品图片");
        easybuy_productMapper.insertSelective(easybuy_product);
        for(String images:productImageString){
            Easybuy_product_images productImages = new Easybuy_product_images();
            productImages.setFilename(images);
            productImages.setPid(easybuy_product.getId());
            easybuy_product_imagesMapper.insertSelective(productImages);
        }
    }

    @Override
    @Transactional(readOnly = true)
    public List<Easybuy_product> findIndexProByCategory(Integer categoryId) throws Exception {
        logger.info("根据id查询首页商品");
        Map<String,Object> pamp = new HashMap<>();
        pamp.put("categoryLevel1Id", categoryId);
        return easybuy_productMapper.selectIndexProd(pamp);
    }

    @Override
    public Easybuy_product selectByPrimaryKey(Integer id) {
        Easybuy_product product = easybuy_productMapper.selectByPrimaryKey(id);
        return product;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void addProductCarToRedis(Easybuy_user user, Easybuy_product product, Integer number) throws Exception {
        logger.info("把商品添加到redis购物车");
        if(redisTemplate.boundHashOps("user_car_"+user.getId()).hasKey(product.getId()+"")){
          String carShopStr =  (String) redisTemplate.boundHashOps("user_car_"+user.getId()).get(product.getId()+"");
           CarShop carShop = JsonUtil.jsonToPojo(carShopStr, CarShop.class);
            carShop.setBuyNum(carShop.getBuyNum() +number);
            carShopStr = JsonUtil.objectToJson(carShop);
            redisTemplate.boundHashOps("user_car_"+user.getId()).put(product.getId()+"",carShopStr);

        }else{
            //第一次购买
            CarShop carShop =  new CarShop();
            carShop.setProduct(product);
            carShop.setBuyNum(number);
            String carShopStr = JsonUtil.objectToJson(carShop);
            redisTemplate.boundHashOps("user_car_"+user.getId()).put(product.getId()+"",carShopStr);
        }
    }

    @Override
    public List<CarShop> findProdFromRedis(Easybuy_user user) throws Exception {
        logger.info("利用用户ID获取该用户的购物车列表");
      //使用hash保存所有的用户ID为该用户的购物KEY，选项 key 商品ID，value 该商品的信息
      //获取购物车的所有的值，是一个json字符串的集合
        List<Object> carShopStrList =(List<Object>) redisTemplate.boundHashOps("user_car_"+user.getId()).values();
        List<CarShop> carShopList = new ArrayList<>() ;
        for(Object obj :carShopStrList){
            CarShop carShop = JsonUtil.jsonToPojo( obj.toString(),CarShop.class ) ;
            carShopList.add(carShop) ;
        }
        return carShopList;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void deleteCarShopFromRedis(Easybuy_user user, Integer pid) throws Exception {
        redisTemplate.boundHashOps("user_car_"+user.getId()).delete(pid.toString());
    }

    @Override
    public CarShop findCarFromRedisByUidAndPid(Integer uid, Integer pid) throws Exception {
       String carShopStr = (String)redisTemplate.boundHashOps("user_car_"+uid).get(pid+"");
        CarShop carShop = JsonUtil.jsonToPojo(carShopStr, CarShop.class);
        return carShop;
    }


}
